﻿Public Class AddConsumptionService


  Private mLastCounter As Integer = 0
  Public Property LastCounter() As Integer
    Get
      Return mLastCounter
    End Get
    Set(ByVal value As Integer)
      mLastCounter = value
    End Set
  End Property


  Public Function AddConsumption(ByVal lStr As String, ByVal lSelectedUser As UserCode, ByVal lDateTime As DateTime, ByVal ltComment As String) As DailyConsumptionVM

    lStr = lStr.Replace(",", ".")
    Dim lValue As New DailyConsumption '= DB.Context.DailyConsumption.CreateObject
    With lValue
      .Comment = ltComment
      .DateDC = lDateTime
      .DayOfYear = lDateTime.DayOfYear + LastCounter
      .Year = DB.Context.Year.Where(Function(f) f.Value = lDateTime.Year).FirstOrDefault
      If .Year Is Nothing Then
        .Year = New Year With {.Value = lDateTime.Year}
      End If
      .Consumption = Val(lStr) '(Now - StartDate).TotalMilliseconds ' Now.Ticks 'Int(Now.Ticks / 100000.0)
      .UserCode = lSelectedUser
    End With

    DB.Context.DailyConsumption.AddObject(lValue)
    Return New DailyConsumptionVM(lValue)
  End Function
End Class
